package com.gbs.info2;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.gbs.info2.tools.ConfigFileHelper;

public class GBSinfo2Activity extends Activity implements View.OnClickListener {
	Button okBtn, newPinBtn;
	EditText pinFied, newPinField, newPinField2;
	TextView welcomePinMsg;
	LinearLayout newPinBox, pinBox;
	private ConfigFileHelper configFileHelper;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		configFileHelper = new ConfigFileHelper(this);
		okBtn = (Button) findViewById(R.id.okBtn);
		newPinBtn = (Button)findViewById(R.id.newPinBtn);
		pinFied = (EditText)findViewById(R.id.pinField);
		newPinField = (EditText)findViewById(R.id.newPinField);
		newPinField2 = (EditText) findViewById(R.id.newPinField2);
		pinBox = (LinearLayout)findViewById(R.id.pinBox);
		newPinBox = (LinearLayout)findViewById(R.id.newPinBox);
		welcomePinMsg = (TextView)findViewById(R.id.pinMsgLab);
		okBtn.setOnClickListener(this);
		newPinBtn.setOnClickListener(this);
		if (configFileHelper.isConfigFileExist()){
			newPinBox.setVisibility(-1);
		}else {
				welcomePinMsg.setText(getString(R.string.createNewPinMsg));
				newPinField.requestFocus();
			pinBox.setVisibility(-1);
		}
		
		//startActivity(new Intent (this, VehicleList.class));
	}
	public void onClick(View view) {
		if (newPinBtn.getId() == ((Button)view).getId()){
					// overenie zhody novych pinov
					if (newPinField.getText().toString().equals(newPinField2.getText().toString())) {
						// overenie dlzky noveho pinu
						if (newPinField.getText().toString().length() > 3) {
							//vytvorenie datoveho subora
							configFileHelper.createAppDatFile(null);
							//zapis pinu
							configFileHelper.writeLine(1, configFileHelper.getMd5Digest(newPinField.getText().toString()));
							Toast.makeText(this, getString(R.string.pinOKchanged),2000).show();
							finish();
							startActivity(new Intent (this, VehicleList.class));
						} else {
							Toast.makeText(this,getString(R.string.pinLengthFailed), 2000).show();
						}
					} else {
						Toast.makeText(this, getString(R.string.pinNoMatch), 2000).show();
					}
				}
		if(okBtn.getId() == ((Button)view).getId()){
			if (configFileHelper.getMd5Digest(pinFied.getText().toString()).equals(configFileHelper.readLine(1))){
				Toast.makeText(GBSinfo2Activity.this, getString(R.string.pinOKtoast), 2000).show();
				finish();
				Intent intentVehicleList = new Intent (this, VehicleList.class) ;
				Bundle bundle = new Bundle();
				bundle.putSerializable("autoUpdate", configFileHelper.readLine(3));
				intentVehicleList.putExtras(bundle);
				startActivity(intentVehicleList);
			}else {
				Toast.makeText(GBSinfo2Activity.this, getString(R.string.pinFailedToast), 2000).show();
			}
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.startmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.mnuAbout:;
		startActivity(new Intent(this, About.class));
		return true;
		default:
		return super.onOptionsItemSelected(item);
		}
	}
	
	@Override
	public void onBackPressed() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getString(R.string.exitConfirm))
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setCancelable(false)
				.setPositiveButton(getString(R.string.yesAnswer),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								finish();
								System.exit(0);
							}
						})
				.setNegativeButton(getString(R.string.noAnswer),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();

	}
}